package com.example.front.mapper;

import com.example.common.constant.AndonCode;
import com.example.common.dto.Pagination;
import com.example.front.dto.IncidentDto;
import com.example.front.dto.IncidentSearchDto;
import com.example.front.entity.Gateway;
import com.example.front.entity.Incident;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface IncidentMapper {

    @Insert("INSERT INTO `incident`" +
            " (`tenant_id`,`user_id` , `event_id`, `workshop_id`, `line_id`, `box_id`, `button_id`,`level_id`,`processing_status`) VALUES" +
            " (#{tenantId}, #{userId}, #{eventId}, #{workshopId}, #{lineId}, #{boxId}, #{buttonId},#{levelId},#{processingStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    void create(Incident incident);

    @Select("<script>" +
            "SELECT * FROM `incident`" +
            "<where>" +
            " `tenant_id`=#{incident.tenantId} " +
            "<if test='incident.eventId!=null'> AND `event_id`=#{incident.eventId} </if>" +
            "<if test='incident.workshopId!=null'> AND `workshop_id`=#{incident.workshopId} </if>" +
            "<if test='incident.lineId!=null'> AND `line_id`=#{incident.lineId} </if>" +
            "<if test='incident.boxId!=null'> AND `box_id`=#{incident.boxId} </if>" +
            "<if test='incident.buttonId!=null'> AND `button_id`=#{incident.buttonId} </if>" +
            "</where> AND `status`!=" + User.STATUS_DELETE+
            " ORDER BY `id` DESC  LIMIT 1" +
            "</script>")
    Incident checkCreate(@Param("incident") Incident incident);

    @Update("UPDATE `incident` SET " +
            "`response_at` = #{incident.responseAt}, " +
            "`processing_status` = #{incident.processingStatus} " +
            " WHERE `id`=#{incident.id} AND `tenant_id`=#{incident.tenantId} AND `status`!=" + User.STATUS_DELETE)
    int startResponse(@Param("incident") Incident incident);

    @Update("UPDATE `incident` SET " +
            "`complete_at` = #{incident.completeAt}, " +
            "`processing_status` = #{incident.processingStatus}" +
            " WHERE `id`=#{incident.id} AND `tenant_id`=#{incident.tenantId} AND `status`!=" + User.STATUS_DELETE)
    int finish(@Param("incident") Incident incident);

    @Update("UPDATE `incident` SET " +
            "`user_id` = #{incidentDto.userId} " +
            ", `solution` = #{incidentDto.solution}" +
            " WHERE `id`=#{incidentDto.id} AND `tenant_id`=#{incidentDto.tenantId}  AND `status`!=" + User.STATUS_DELETE )
    int update(@Param("incidentDto") IncidentDto incidentDto);

    @Update("UPDATE `incident` SET " +
            "`text` = #{incidentDto.text} " +
            " WHERE `id`=#{incidentDto.id}")
    int updateText(@Param("incidentDto") IncidentDto incidentDto);

    @Update("UPDATE `incident` SET " +
            "`processing_status` = #{incident.processingStatus} " +
            " WHERE `id`=#{incident.id} AND `tenant_id`=#{incident.tenantId}  AND `status`!=" + User.STATUS_DELETE )
    int updateProcessingStatus(@Param("incident") Incident incident);

    @Update("UPDATE `incident` SET " +
            "`processing_status` = #{incident.processingStatus}, " +
            "`complete_at`=now()" +
            " WHERE `id`=#{incident.id} AND `tenant_id`=#{incident.tenantId}  AND `status`!=" + User.STATUS_DELETE )
    int updateCompleteAt(@Param("incident") Incident incident);

    @Update("UPDATE `incident` SET " +
            "`user_id` = #{incident.userId}" +
            " WHERE `id`=#{incident.id} AND `tenant_id`=#{incident.tenantId}  AND `status`!=" + User.STATUS_DELETE )
    int updateUserId(@Param("incident") Incident incident);

    @Update("UPDATE `incident` SET " +
            "`level_id` = #{LevelId} " +
            " WHERE `id`=#{id}  AND `status`!=" + User.STATUS_DELETE )
    int updateLevelId(@Param("id") Long id,@Param("LevelId") Long LevelId);

    @Select("<script>" +
            "SELECT * FROM `incident`" +
            "<where>" +
            "<if test='condition.processingStatus!=0'> AND `processing_status`=#{condition.processingStatus}</if>" +
            "<if test='condition.id!=null'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.eventId!=null'> AND `event_id`=#{condition.eventId}</if>" +
            "<if test='condition.workshopId!=null'> AND `workshop_id`=#{condition.workshopId}</if>" +
            "<if test='condition.lineId!=null'> AND `line_id`=#{condition.lineId}</if>" +
            "<if test='condition.startTime!=\"\" and condition.endTime!=\"\" and condition.startTime!=null and condition.endTime!=null'> AND created_at between #{condition.startTime} and #{condition.endTime}</if>" +
            "<if test='condition.startTime==\"\" and condition.endTime!=\"\"'> AND created_at gt #{condition.startTime}</if>" +
            "<if test='condition.startTime!=\"\" and condition.endTime==\"\"'> AND created_at lt #{condition.endTime}</if>" +
            "<if test='condition.boxId!=null'> AND `box_id`=#{condition.boxId}</if>" +
            "<if test='condition.buttonId!=null'> AND `button_id` LIKE CONCAT('%',#{condition.buttonId},'%') </if>" +
            " AND `tenant_id`=#{condition.tenantId} " +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            " ORDER BY `created_at` DESC LIMIT #{pagination.offset}, #{pagination.limit}" +
            "</script>")
    List<Incident> findAll(@Param("condition") IncidentSearchDto condition, @Param("pagination") Pagination pagination);

    @Select("<script>" +
            "SELECT * FROM `incident`" +
            "where response_at is null  AND `status`!=" + User.STATUS_DELETE +
            " ORDER BY `created_at` DESC" +
            "</script>")
    List<Incident> findCreatedAt();

    @Select("<script>" +
            "SELECT COUNT(*) FROM `incident`" +
            "<where>" +
            "<if test='condition.processingStatus!=0'> AND `processing_status`=#{condition.processingStatus}</if>" +
            "<if test='condition.id!=null'> AND `id`=#{condition.id}</if>" +
            "<if test='condition.eventId!=null'> AND `event_id`=#{condition.eventId}</if>" +
            "<if test='condition.workshopId!=null'> AND `workshop_id`=#{condition.workshopId}</if>" +
            "<if test='condition.lineId!=null'> AND `line_id`=#{condition.lineId}</if>" +
            "<if test='condition.startTime!=\"\" and condition.endTime!=\"\" and condition.startTime!= null and condition.endTime!= null'> AND created_at between #{condition.startTime} and #{condition.endTime}</if>" +
            "<if test='condition.startTime==\"\" and condition.endTime!=\"\"'> AND created_at gt #{condition.startTime}</if>" +
            "<if test='condition.startTime!=\"\" and condition.endTime==\"\"'> AND created_at lt #{condition.endTime}</if>" +
            "<if test='condition.boxId!=null'> AND `box_id`=#{condition.boxId}</if>" +
            "<if test='condition.buttonId!=null'> AND `button_id` LIKE CONCAT('%',#{condition.buttonId},'%') </if>" +
            " AND `tenant_id`=#{condition.tenantId} " +
            "</where> AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    long count(@Param("condition") IncidentSearchDto condition);

    @Update("UPDATE `incident` SET `status` ="+ User.STATUS_DELETE+
            " WHERE `id`=#{id} AND `tenant_id`=#{tenantId} AND `status`!=" + User.STATUS_DELETE)
    int deleteById(long id, long tenantId);


    @Select("SELECT * FROM gateway WHERE `tenant_id` = #{tenantId} AND `ip` = #{ip} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Incident findByIp(@Param("tenantId") long tenantId, @Param("ip") String ip);

    @Select("SELECT * FROM gateway WHERE `tenant_id` = #{tenantId} AND `id` = #{id}  AND `name` = #{name} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Incident findByIdName(@Param("tenantId") long tenantId, @Param("id") int id, @Param("name") String name);

    @Select("SELECT * FROM gateway WHERE `tenant_id` = #{tenantId} AND `id` = #{id}  AND `ip` = #{ip} AND `status`!=" + User.STATUS_DELETE+" LIMIT 1")
    Incident findByIdIp(@Param("tenantId") long tenantId, @Param("id") int id, @Param("ip") String ip);

    @Select("SELECT * FROM `incident` WHERE `box_id`=#{boxId} and `complete_at` IS NULL")
    List<Incident> selectCancel(@Param("boxId") long boxId);

    @Update("UPDATE `incident` SET `complete_at` = #{completeAt} , `processing_status` = #{processingStatus}"+
            " WHERE `box_id`=#{boxId} and `complete_at` IS NULL")
    int cancel(@Param("boxId") long boxId, String completeAt,Integer processingStatus);

    @Select("<script>" +
            "SELECT *,count(*) as count FROM `incident`" +
            "where `tenant_id`=#{incident.tenantId} AND `box_id` in " +
            "<foreach item='boxId' index='index' collection='incident.boxIds' open='(' separator=',' close=')'>" +
            "#{boxId}" +
            "</foreach>" +
            " AND `status`!=" + User.STATUS_DELETE +
            " AND processing_status != 3 " +
            " AND created_at BETWEEN #{incident.dateStart} and #{incident.dateEnd} " +
            " GROUP BY event_id , box_id ORDER BY `created_at` DESC" +
            "</script>")
    List<IncidentDto> findByBoxId(@Param("incident") Incident incident);

}
